# -*- coding: utf-8 -*-

#(c)Jonathan & Gregoire (read __doc__ for more information)
#Copyright (C)%(author)s (%(date)s)
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

## @packagename PyCol
# PyCol it is a card game, you can play against human player or computer
# PyCol is a french wordplay "picole"
# Objective game's is create most structure, the player who close one structure he wins 1 point
# The winner is the player with the most points


import  sys
import  random
from    engine.pattern.cards.Card1 import Card1

__author__      = \
"""
Jonathan MERCIER <bioinfornatics at gmail.com >
Gregoire NAUDIN  <greg.naudin at gmail.com    >

"""
__version__     = "$Revision: 0.1 $"
__date__        = "$Date: 2009/23/09$"
__copyright__   = "Copyright (c) 2009 Jonathan MERCIER and Gregoire NAUDIN"
__license__     = "GPL v3 or under"
__name__        = "Grid.py"
__description__ = \
"""
Grid it is an object for store card object
"""
from    engine.Singleton    import Singleton

## Documentation for Grid class
# This class is a structure for store object card
class Grid(Singleton) :
    def __init__(self,x_length=10,y_length=5):
        self.__x_length         = x_length
        self.__y_length         = y_length
        self.__grid             = grid = [[None for column in range(self.__x_length)] for line in range(self.__y_length)]
        self.__card_in_grid     = 0

    ## Public getter x_length
    # @param self The object pointer
    def get_x_length(self):
        return self.__x_length
    
    ## Private setter x_length
    # @param self The object pointer
    # @param value The newest value
    def __set_x_length(self,value):
        self.__x_length = value
        
    ## Public getter y_length
    # @param self The object pointer
    def get_y_length(self):
        return self.__y_length
    
    ## Private setter y_length
    # @param self The object pointer
    # @param value The newest value
    def __set_y_length(self,value):
        self.__y_length = value

    ## A generic answer for private attribute
    # @param self The object pointer
    def private(self):
        print u"this attribute is read only"

    ## Property for x_length
    x_length = property(get_x_length,private)

    ## Property for y_length
    y_length = property(get_y_length,private)

    ## Public method get_grid
    # @param self The pointer object
    # @return: self.__grid the 2D list where is store cards
    def get_grid(self):
        return [[card for card in self.__grid[line]] for line in range (len(self.__grid))]
    
    ## Public method set_grid
    # @param self The pointer object
    # @param value: the newest pointer of grid object
    def set_grid(self,value):
        self.__grid = value
        
    ## Property for grid
    grid = property(get_grid,set_grid)

    ## Public method get_card
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card(self,x,y):
        return self.__grid[y][x]

    ## Public method get_card_border_middle
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_middle(self,x,y):
        return self.__grid[y][x].get_middle()
    
    ## Public method get_card_border_up_left
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    
    def get_card_border_up_left(self,x,y):
        return self.__grid[y][x].get_border_up_left()
    
    ## Public method get_card_border_up
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_up(self,x,y):
        return self.__grid[y][x].get_border_up()
 
    ## Public method get_card_border_up_right
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_up_right(self,x,y):
        return self.__grid[y][x].get_border_up_right()
       
    ## Public method get_card_border_down_left
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_down_left(self,x,y):
        return self.__grid[y][x].get_border_down_left()
       
    ## Public method get_card_border_down
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_down(self,x,y):
        return self.__grid[y][x].get_border_down()
       
    ## Public method get_card_border_down_right
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_down_right(self,x,y):
        return self.__grid[y][x].get_border_down_right()

    ## Public method get_card_border_left
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_left(self,x,y):
        return self.__grid[y][x].get_border_left()
    
    ## Public method get_card_border_right
    # @param self The pointer object
    # @param x The x coordinate
    # @param y The y coordinate
    def get_card_border_right(self,x,y):
        return self.__grid[y][x].get_border_right()

    ## Public getter for nb_plain
    def get_card_nb_plain(self,x,y):
        return self.__grid[y][x].get_nb_plain()
    ## Private setter for nb_plain
    def set_card_nb_plain(self,x,y,value):
        self.__grid[y][x].set_nb_plain(value)
        
    ## Public getter for nb_river
    def get_card_nb_river(self,x,y):
        return self.__grid[y][x].get_nb_river()
    ## Private setter for nb_river
    def set_card_nb_river(self,x,y,value):
        self.__grid[y][x].set_nb_river(value)
        
    ## Public getter for nb_forest
    def get_card_nb_forest(self,x,y):
        return self.__grid[y][x].get_nb_forest()
    ## Private setter for nb_forest
    def set_card_nb_forest(self,x,y,value):
        self.__grid[y][x].set_nb_forest(value)

    ## Public method add_card
    # @param self The pointer object
    # @param card A card object
    # @param x The x coordinate
    # @param y The y coordinate
    def add_card(self,card,x,y):
        self.__grid[y][x] = card
        self.__card_in_grid += 1
    
    ## Public method add_start_card
    # Add a start card in random position
    # @param self The self object pointer
    def add_start_card(self):
        x,y = self.__random_start()
        card = Card1()
        self.add_card(card,x,y)
        self.__card_in_grid += 1
    
    ## Private Method __random_start
    # @param self The object pointer
    def __random_start(self):
        x = random.randint(1,(self.__x_length-2))
        y = random.randint(1,(self.__y_length-2))
        return x,y

    def is_full(self):
        is_full = False
        if self.__x_length * self.__y_length == self.__card_in_grid:
            is_full = True
        else:
            is_full = False
        return is_full